package com.example.scale;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import android.util.Log;

public class ResourcesManager {
	// Global Variables

	private static final ResourcesManager INSTANCE = new ResourcesManager();

	public Engine engine;
	public GameActivity activity;
	public ZoomCamera camera;
	public VertexBufferObjectManager vbom;
	
	// Splash Screen Textures
	public ITextureRegion splash_region;
	private BitmapTextureAtlas splashTextureAtlas;

	// Menu Screen Textures
	public ITextureRegion menu_background_region;
	public ITextureRegion play_region;
	public ITextureRegion options_region;
	public ITextureRegion skills_region;
	private BuildableBitmapTextureAtlas menuTextureAtlas;

	public Font font;

	// Player Sprite Sheet Textures
	public BuildableBitmapTextureAtlas playerTextureAtlas;
	public ITiledTextureRegion player_texture_region;
	public BuildableBitmapTextureAtlas playerHoistingTextureAtlas;
	public ITiledTextureRegion player_hoisting_region;
	public BuildableBitmapTextureAtlas playerJumpingRunTextureAtlas;
	public ITiledTextureRegion player_jumping_run_region;
	public BuildableBitmapTextureAtlas playerJumpingUpTextureAtlas;
	public ITiledTextureRegion player_jumping_up_region;
	public BuildableBitmapTextureAtlas playerRunningTextureAtlas;
	public ITiledTextureRegion player_running_region;
	public BuildableBitmapTextureAtlas playerWalkingAndStoppedTextureAtlas;
	public ITiledTextureRegion player_walking_region;
	public ITiledTextureRegion player_stopped_region;
	
	// Game Screen Textures
	public BuildableBitmapTextureAtlas gameTextureAtlas;
	public static ITiledTextureRegion pause_button_region;
	public ITiledTextureRegion character_region;
	public BuildableBitmapTextureAtlas gameTextureAtlas2;
	public ITextureRegion powerup_region;
	public ITiledTextureRegion turret_region;
	public ITextureRegion turret_base_region;
	public ITextureRegion dialogue_card_region;

	// Joystick textures
	public static ITextureRegion knob_region;
	public static ITextureRegion base_region;
	public static BitmapTextureAtlas controlTextureRegion;
	
	// Level select screen Textures
	public BuildableBitmapTextureAtlas LevelSelectAtlas;
	public ITiledTextureRegion complete_numbers_region;
	public ITextureRegion level_select_header_region;
	
	// Options screen Textures
	public BuildableBitmapTextureAtlas OptionsScreenAtlas;
	public ITiledTextureRegion options_checkbox_tiled_region;
	public ITextureRegion options_toggle_plot_region;
	public ITextureRegion save_settings_region;
	public ITiledTextureRegion control_method_tiled_region;
	public ITextureRegion yes_region;
	public ITextureRegion no_region;
	
	// General Resources
	public BuildableBitmapTextureAtlas GeneralAtlas;
	public ITextureRegion arrow_region;
	public ITextureRegion accept_region;
	public ITextureRegion cancel_region;
	public ITextureRegion blank_background_region;
	public BuildableBitmapTextureAtlas BackgroundAtlas;
	
	// Load any textures that need to be used on more than one screen
	public void loadGeneralResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		GeneralAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
		arrow_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(GeneralAtlas, activity, "ArrowTransparent.png");
		options_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(GeneralAtlas, activity, "newOptionsButton.png");
		skills_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(GeneralAtlas, activity, "newSkillsButton.png");
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/text/");
		accept_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(GeneralAtlas, activity, "Accept.png");
		cancel_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(GeneralAtlas, activity, "Cancel.png");
		yes_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(GeneralAtlas, activity, "Yes.png");
		no_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(GeneralAtlas, activity, "No.png");
		
		try {
			this.GeneralAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.GeneralAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		BackgroundAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 960, 540, TextureOptions.BILINEAR);
		blank_background_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(BackgroundAtlas, activity,
						"blankBackground.png");
		
		try {
			this.BackgroundAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.BackgroundAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
	}	

	public void loadGameResources() {
		loadGameGraphics();
		loadGameFonts();
		loadGameAudio();
	}

	public void loadMenuResources() {
		loadMenuGraphics();
		loadMenuAudio();
		loadMenuFonts();
	}
	
	public void loadLevelSelectResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/levelSelect/");
		LevelSelectAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 128, TextureOptions.BILINEAR);
		complete_numbers_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(LevelSelectAtlas, activity, "NumbersSpriteSheetSmall.png", 11, 1);
		level_select_header_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(LevelSelectAtlas, activity, "LevelSelectResized.png");
		
		try {
			this.LevelSelectAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.LevelSelectAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
	}

	private void loadMenuGraphics() {
		// Create a buildable bitmap texture atlas so we don't have to specify
		// positions of particular graphics inside the texture
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
		menuTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		menu_background_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity,
						"Scale_Menu_Background.png");
		play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				menuTextureAtlas, activity, "newPlayButton.png");

		try {
			this.menuTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.menuTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
	}
	
	public void loadOptionsGraphics() {
		OptionsScreenAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/options/");
		options_toggle_plot_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(OptionsScreenAtlas, activity, "togglePlotButtonResized.png");
		options_checkbox_tiled_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(OptionsScreenAtlas, activity, "checkBoxSpriteSheet.png", 2, 1);
		control_method_tiled_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(OptionsScreenAtlas, activity, "ControlMethodSpriteSheet.png", 3, 1);
		save_settings_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(OptionsScreenAtlas, activity, "SaveSettingsButtonResized.png");
		
		try {
			this.OptionsScreenAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.OptionsScreenAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
	}

	private void loadMenuFonts() {
		FontFactory.setAssetBasePath("font/");
		final ITexture mainFontTexture = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		font = FontFactory.createStrokeFromAsset(activity.getFontManager(),
				mainFontTexture, activity.getAssets(), "BebasNeue.otf", 50,
				true, android.graphics.Color.WHITE, 2,
				android.graphics.Color.BLACK);
		font.load();
	}

	private void loadMenuAudio() {

	}
	
	private void switchPlayerTexture(String newTexturePath, int columns, int rows) {
		playerTextureAtlas.clearTextureAtlasSources();
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/Player/");
		player_texture_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerTextureAtlas, activity, newTexturePath, columns, rows);

        //this.engine.getTextureManager().loadTexture(this.playerTextureAtlas);

		try {
			this.playerTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			//this.playerTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
	}

	public void switchToHoistingTexture() {
		switchPlayerTexture("HoistingSpriteSheet.png", 6, 3);
	}

	public void switchToJumpingForwardTexture() {
		switchPlayerTexture("JumpingRunSpriteSheet.png", 6, 3);
	}

	public void switchToJumpingUpTexture() {
		switchPlayerTexture("JumpingUpSpriteSheet.png", 6, 2);
	}

	public void switchToRunningTexture() {
		//playerTextureAtlas.clearTextureAtlasSources();
		//player_texture_region = player_running_region;
		switchPlayerTexture("RunningSpriteSheet.png", 6, 3);
	}
	
	public void switchToWalkingTexture() {
		switchPlayerTexture("WalkingSpriteSheet.png", 6, 2);
	}
	
	public void switchToStoppedTexture() {
		//playerTextureAtlas.clearTextureAtlasSources();
		//player_texture_region = player_stopped_region;
		switchPlayerTexture("StoppedSpriteSheet.png", 2, 1);
	}
	
	private void loadGameGraphics() {
		// Load the player sprite sheets
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/Player/");
		
		playerTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		//player_texture_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerTextureAtlas, activity, "StoppedSpriteSheet.png", 2, 1);
		player_texture_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerTextureAtlas, activity, "RunningSpriteSheet.png", 6, 3);
		player_texture_region.setCurrentTileIndex(0);
		try {
			this.playerTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.playerTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		playerHoistingTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		player_hoisting_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerHoistingTextureAtlas, activity, "HoistingSpriteSheet.png", 6, 3); // Only 16 frames filled
		player_hoisting_region.setCurrentTileIndex(0);
		try {
			this.playerHoistingTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.playerHoistingTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		playerJumpingRunTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		player_jumping_run_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerJumpingRunTextureAtlas, activity, "JumpingRunSpriteSheet.png", 6, 3); // 18 frames
		player_jumping_run_region.setCurrentTileIndex(0);
		try {
			this.playerJumpingRunTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.playerJumpingRunTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		playerJumpingUpTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		player_jumping_up_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerJumpingUpTextureAtlas, activity, "JumpingUpSpriteSheet.png", 6, 2); // Only 11 frames filled
		player_jumping_up_region.setCurrentTileIndex(0);
		try {
			this.playerJumpingUpTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.playerJumpingUpTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		playerRunningTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		player_running_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerRunningTextureAtlas, activity, "RunningSpriteSheet.png", 6, 3); // Only 13 frames filled
		player_running_region.setCurrentTileIndex(0);
		try {
			this.playerRunningTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.playerRunningTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		playerWalkingAndStoppedTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		player_walking_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerWalkingAndStoppedTextureAtlas, activity, "WalkingSpriteSheet.png", 6, 2); // Only 10 frames filled
		player_walking_region.setCurrentTileIndex(0);
		player_stopped_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerWalkingAndStoppedTextureAtlas, activity, "StoppedSpriteSheet.png", 2, 1); // 2 frames
		player_stopped_region.setCurrentTileIndex(0);
		try {
			this.playerWalkingAndStoppedTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.playerWalkingAndStoppedTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		// Create new textures and load 4 texture regions for platforms and coin
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
		gameTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		
		character_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameTextureAtlas, activity, "SpriteSheetFinal.png", 6, 3);
		character_region.setCurrentTileIndex(0);
		
		pause_button_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameTextureAtlas, activity, "pausePlaySpriteSheet.png", 2, 1);
		pause_button_region.setCurrentTileIndex(0);
		
		try {
			this.gameTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.gameTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
		gameTextureAtlas2 = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		powerup_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas2, activity, "PowerupTransparent.png");
		
		// TODO: Too many items in this atlas
		dialogue_card_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas2, activity, "DialogueCardResized.png");
		
		turret_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameTextureAtlas2, activity, "TurretSpriteSheet.png", 2, 1);
		turret_region.setCurrentTileIndex(0);
		
		turret_base_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas2, activity, "Turret_Base.png");
				
		try {
			this.gameTextureAtlas2
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.gameTextureAtlas2.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}

		// Create the texture regions for the joystick
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
		controlTextureRegion = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
		base_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				controlTextureRegion, activity, "small_base.png",
				0, 0);
		knob_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				controlTextureRegion, activity, "small_knob.png",
				128, 0);
		controlTextureRegion.load();
	}

	private void loadGameFonts() {

	}

	private void loadGameAudio() {

	}

	public void loadSplashScreen() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		splashTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 800, 480, TextureOptions.BILINEAR);
		splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				splashTextureAtlas, activity, "SplashScreen.png", 0, 0);

		splashTextureAtlas.load();
	}

	public void unloadSplashScreen() {
		splashTextureAtlas.unload();
		splash_region = null;
	}

	/**
	 * @param engine
	 * @param activity
	 * @param camera
	 * @param vbom
	 * <br>
	 * <br>
	 *            We use this method at beginning of game loading, to prepare
	 *            Resources Manager properly, setting all needed parameters, so
	 *            we can latter access them from different classes (eg. scenes)
	 */

	public static ResourcesManager getInstance() {
		return INSTANCE;
	}

	public static void prepareManager(Engine mEngine, GameActivity activity2,
			ZoomCamera camera2,
			VertexBufferObjectManager vertexBufferObjectManager) {
		getInstance().engine = mEngine;
		getInstance().activity = activity2;
		getInstance().camera = camera2;
		getInstance().vbom = vertexBufferObjectManager;
	}

	public void unloadMenuTextures() {
	}

	public void loadMenuTextures() {
		menuTextureAtlas.load();
	}

	public void unloadGameTextures() {
		gameTextureAtlas.unload();
	}

	public void unloadLevelSelectTextures() {
		LevelSelectAtlas.unload();
	}
	
	public void unloadOptionsTextures() {
		OptionsScreenAtlas.unload();
	}
	
}